package com.qilin.hammer;

import org.apache.http.impl.client.DefaultHttpClient;
import org.junit.BeforeClass;
import org.restlet.data.Form;
import org.restlet.representation.Representation;
import org.restlet.resource.ClientResource;

import com.vitria.m3oui.rest.app.mgr.HttpClient;
import com.vitria.m3oui.security.XXTEA;

import junit.framework.TestCase;

public class RuntimeErrorTest extends TestCase{
	
	public static String APP_ID = "/px";
    public static String APP_URL_PREFIX = "/m3oui/rest/app.mgr/apps";
    
    public static String INSTALL_UID = "AAAAAAAA-AAAA-AAAA-AAAA-AAAAAAAAAPX";
    
    private DefaultHttpClient httpclient = null;
    private Integer threshold = 0;
    
    @BeforeClass 
    protected void setUp() throws Exception {
    	httpclient = HttpClient.login("vtbaadmin", "vitria");
    }
    
	public void testInstallLoop() throws Exception{
		String opString = null;
		final String callpx = "/m3oui/rest/app/px/user/vtbaadmin/pxm/px?op=getRuntimeInfo";
		int threadNum = 100;
		int loops = 500;
		for(int i=0; i<loops+1; i++){
			synchronized(threshold){
				if(threshold == 0){
    				if(i == loops)
    					break;
    				threshold = threadNum;
    				System.out.println("Loop:"+i);
    			} else {
    				i--;
    				continue;
    			}
			}
			opString = APP_URL_PREFIX+APP_ID+"?op=uninstall";
			HttpClient.get(httpclient, opString);
			opString = APP_URL_PREFIX+APP_ID+"?op=install&uid="+INSTALL_UID;
			HttpClient.get(httpclient, opString);
			for(int j=0; j<threadNum; j++){
				new Thread(){
    				public void run(){
    					try {
    						HttpClient.get(httpclient,callpx);
    						synchronized(threshold){
				    			threshold--;
				    		}
    					} catch (Exception e) {
							e.printStackTrace();
						}
    				}
    			}.start();
			}
		}
	}
	
}
